package servlet.article;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Article;
import service.ArticleService;
import util.IpUtil;

/**
 * Servlet implementation class ArticleAddServlet
 */
@WebServlet("/ArticleAddServlet")
public class ArticleAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("article/article_add.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收article_add.jsp页面的表单
		
		Article article = new Article();
		
		article.setId(request.getParameter("id"));
		article.setContent(request.getParameter("content"));
		article.setPostTime(new Date());
		article.setIp(IpUtil.getIp(request));
		
		//article集合的添加
		ArticleService service = new ArticleService();
		service.save(article);
		//跳转
		response.sendRedirect("ArticleListServlet");
	}

	public static void main(String[] args) {
		System.out.println(LocalDate.now());
		System.out.println(LocalTime.now());
		System.out.println(LocalDateTime.now());
	}
}
